package com.itheima.entry.service.impl;

import com.itheima.entry.dao.IStudentDAO;
import com.itheima.entry.domain.Student;
import com.itheima.entry.factory.StudentFactroy;
import com.itheima.entry.service.IStatisticsService;

import java.util.ArrayList;
import java.util.TreeMap;


public class StatisticsServiceImpl implements IStatisticsService {
    private static IStudentDAO dao = StudentFactroy.getDao();

    @Override
    public TreeMap<String, Integer> boyAndGirl() {   //查询男女各多少人,存入 TreeMap
        TreeMap<String, Integer> map = new TreeMap<>();
        ArrayList<Student> list = dao.allStudents();
        int count1 = 0;
        int count2 = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null && stu.getGender().equals("男")) {
                count1++;
            } else {
                count2++;
            }
        }
        map.put("男", count1);
        map.put("女", count2);
//        System.out.println("男\t\t\t" + count1 + "人");
//        System.out.println("女\t\t\t" + count2 + "人");
        return map;
    }

    @Override
    public TreeMap<String, Integer> ageInfo() {        //统计各年龄段信息,平均年龄 ,存入TreeMap
        TreeMap<String, Integer> map = new TreeMap<>();
        ArrayList<Student> list = dao.allStudents();
        int count80 = 0;
        int count90 = 0;
        int count00 = 0;
        for (Student stu : list) {
            if (stu.getBirthday().getYear() >= 2000) {
                count00++;
            } else if (stu.getBirthday().getYear() >= 1990 && stu.getBirthday().getYear() < 2000) {
                count90++;
            } else {
                count80++;
            }
        }
        map.put("80后", count80);
        map.put("90后", count90);
        map.put("00后", count00);
//        System.out.println("80后\t\t" + count80 + "人");
//        System.out.println("90后\t\t" + count90 + "人");
//        System.out.println("00后\t\t" + count00 + "人");
        return map;
    }

    @Override
    public TreeMap<Integer, Integer> sumNum() {         //总人数信息  每组人数信息  存入TreeMap
        TreeMap<Integer, Integer> map = new TreeMap<>();
        ArrayList<Student> list = dao.allStudents();
        for (Student stu : list) {
            int group = stu.getGroup();
            boolean flag = map.containsKey(group);
            if (!flag) {//不存在
                map.put(group, 1);
            } else {
                map.put(group, map.get(group) + 1);
            }
        }
//        map.forEach((k, v) -> System.out.println("第" + k + "组\t\t" + v + "人"));
        return map;
    }
}
